package com.zenith.yasoo.catalog;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;

import com.zui.base.BaseJQGridAction;
import com.zui.base.SessionManager;
import com.zui.util.JqGridPage;

public class CatalogAction extends BaseJQGridAction {

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(CatalogAction.class);
	
	private Catalog catalog;
	private List<Catalog> list;
	public CatalogAction() {}
	public CatalogAction(SessionManager sessionManager) {
		this.sessionManager = sessionManager;
	}
	private SessionManager sessionManager;
	public void setSessionManager(SessionManager sessionManager) {
		this.sessionManager = sessionManager;
	}

	/***********************************************************
	 * Business Method start
	 **********************************************************/
	public String index() {
		log.debug("index() method in class CatalogAction is executed!");
		return "index";
	}
	public String editNew() {
		log.debug("editNew() method in class CatalogAction is executed!");
		catalog = new Catalog();
		return "editNew";
	}
	public String create() {
		log.debug("create() method in class CatalogAction is executed!");
		
		catalog.setCreatedByUserLogin(super.getCurrentUserName());
		catalog.setCreatedDate(Calendar.getInstance().getTime());
		catalog.setLastModifiedByUserLogin(catalog.getCreatedByUserLogin());
		catalog.setLastModifiedDate(catalog.getCreatedDate());
		
		if(catalog.getParentCatalog()!=null && catalog.getParentCatalog().getId()==null)
			catalog.setParentCatalog(null);
		sessionManager.saveEntity(catalog);
		return "view";
	}
	public String createAndNew() {
		log.debug("createAndNew() method in class CatalogAction is executed!");
		
		catalog = null;
		return "editNew";
	}
	public String catalogJsonList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Catalog.class);
		if (jq !=null && jq.get_search()) {
			log.debug("defulat search launched for " + Catalog.class
					+ ",searchField:" + jq.getSearchField() + ",searchOper:"
					+ jq.getSearchOper() + ",searchString:"
					+ jq.getSearchString());
		}

		log.debug("catalogJsonList() method in class CatalogAction is executed!");
		list = sessionManager.findByCriteriaForJQGrid(criteria,jq == null ? jq = new JqGridPage()
		: jq);
		return "popupCatalogJsonList";
	}

	/***********************************************************
	 * getters and setters
	 **********************************************************/
	/**
	 * @return the userList
	 */
	public List<Catalog> getlist() {
		return list;
	}
	/**
	 * @param userList the userList to set
	 */
	public void setList(List<Catalog> list) {
		this.list = list;
	}
	/**
	 * @return the catalog
	 */
	public Catalog getCatalog() {
		return catalog;
	}
	/**
	 * @return the list
	 */
	public List<Catalog> getList() {
		return list;
	}
	/**
	 * @param catalog the catalog to set
	 */
	public void setCatalog(Catalog catalog) {
		this.catalog = catalog;
	}
	
}
